﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.DB;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl.Admin
{
    public partial class AdvertiseUC : System.Web.UI.UserControl
    {
        public List<AdvertiseEO> lstAdv
        {
            get { return (List<AdvertiseEO>)ViewState["lstAdv"]; }
            set { ViewState["lstAdv"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Title_FormUC1.setTitle(ConstEx.Table_Name_C.Advertise, ConstEx.funcButtonToolBar.LIST);
                MsgUC1.setClearMsg();
                if (!IsPostBack)
                {
                    //if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                    //{
                    //    ToolBarUC1.Visible = false;
                    //    grvList.Visible = false;
                    //    Commons.setAccessDenied(Page, MsgUC1);
                    //    return;
                    //}
                    ToolBarUC1.ViewGroupButtonByFunction(ConstEx.funcButtonToolBar.OTHER);
                    lstAdv = new List<AdvertiseEO>();
                    string sBackLink = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/Advertise.xml"));
                    lstAdv = (List<AdvertiseEO>)XmlEx.Xml2ListObject(sBackLink, lstAdv.GetType());
                    BindData();
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void EventButtonToolBar_Click(object sender, CustomerEventArgs e)
        {
            switch (e.iType)
            {
                case ConstEx.funcButtonToolBar.INSERT_LIST: break;
                case ConstEx.funcButtonToolBar.INSERT_ITEM: break;
                case ConstEx.funcButtonToolBar.UPDATE_ITEM:
                    SaveChange();
                    break;
                case ConstEx.funcButtonToolBar.DELETE_LIST: break;
                case ConstEx.funcButtonToolBar.DELETE_ITEM: break;
                case ConstEx.funcButtonToolBar.REFRESH_LIST: break;
                case ConstEx.funcButtonToolBar.EXPORT_EXCEL: break;
                case ConstEx.funcButtonToolBar.RESET_FORM: break;
            }
        }

        protected void SaveChange()
        {
            try
            {
                System.IO.File.WriteAllText(Server.MapPath("~/App_Data/Advertise.xml"), XmlEx.ListObject2Xml(lstAdv, ""));
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Cập nhật thành công.");
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        //protected List<BackLinkEO> GetListObject(GridView grv)
        //{
        //    List<BackLinkEO> lstobj = new List<BackLinkEO>();
        //    BackLinkEO obj;
        //    foreach (GridViewRow row in grv.Rows)
        //    {
        //        obj = new BackLinkEO();
        //        obj.PK_iAdvertiseID = Convert.ToInt16(((Label)row.Cells[0].FindControl("lblPK_iAdvertiseID")).Text);
        //        obj.sIcon = ((TextBox)row.Cells[1].FindControl("txtsIcon")).Text;
        //        obj.sTitle = ((TextBox)row.Cells[2].FindControl("txtsTitle")).Text;
        //        obj.sLink = ((TextBox)row.Cells[3].FindControl("txtsLink")).Text;
        //        obj.iStatus = Convert.ToInt16(((DropDownList)row.Cells[4].FindControl("ddliStatus")).SelectedValue);
        //        lstobj.Add(obj);
        //    }
        //    return lstobj;
        //}

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                AdvertiseEO objAdv = new AdvertiseEO();
                TextBox txtsIcon_Temp = (TextBox)grvList.HeaderRow.FindControl("txtsIcon_Temp");
                TextBox txtsTitle_Temp = (TextBox)grvList.HeaderRow.FindControl("txtsTitle_Temp");
                TextBox txtsLink_Temp = (TextBox)grvList.HeaderRow.FindControl("txtsLink_Temp");
                DropDownList ddliStatus_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddliStatus_Temp");
                if (String.IsNullOrEmpty(txtsLink_Temp.Text))
                {
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "loc không được để trống.");
                    txtsLink_Temp.Focus();
                    return;
                }
                objAdv.PK_iAdvertiseID = Convert.ToInt16(0);
                objAdv.sIcon = txtsIcon_Temp.Text;
                objAdv.sTitle = txtsTitle_Temp.Text;
                objAdv.sLink = OtherEx.getUrlStandard(txtsLink_Temp.Text);
                objAdv.iStatus = Convert.ToInt16(ddliStatus_Temp.SelectedValue);
                lstAdv.Insert(0, objAdv);
                txtsIcon_Temp.Text = "";
                txtsTitle_Temp.Text = "";
                txtsLink_Temp.Text = "";
                ddliStatus_Temp.SelectedIndex = 0;
                BindData();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Thêm thành công.");
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            for (Int16 i = 0; i < lstAdv.Count; i++)
            {
                lstAdv[i].PK_iAdvertiseID = Convert.ToInt16(i + 1);
            }
            grvList.DataSource = lstAdv.ToList();
            grvList.DataBind();
            BindData_DDL();
        }

        protected void BindData_DDL()
        {
            DropDownList ddliStatus_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddliStatus_Temp");
            if (ddliStatus_Temp != null)
            {
                ddliStatus_Temp.DataSource = GetListConstants.Category_bStatus_GLC();
                ddliStatus_Temp.DataValueField = "Key";
                ddliStatus_Temp.DataTextField = "Value";
                ddliStatus_Temp.DataBind();
            }
        }

        protected void grvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdDelete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                lstAdv.RemoveAt(id);
                BindData();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Xóa thành công.");
            }
        }

        protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddliStatus = (DropDownList)e.Row.FindControl("ddliStatus");
                Label lbliStatus = (Label)e.Row.FindControl("lbliStatus");
                ddliStatus.DataSource = GetListConstants.Category_bStatus_GLC();
                ddliStatus.DataValueField = "Key";
                ddliStatus.DataTextField = "Value";
                ddliStatus.DataBind();
                ddliStatus.SelectedValue = lbliStatus.Text;
            }
        }
    }
}